package controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import service.army_service;
import entity.info;
import entity.user;

public class edit_army_servlet extends HttpServlet{
	private static final long serialVersionUID = 1L;
	private army_service am_service=new army_service();
	public edit_army_servlet() {
		super();
	}
	public void destroy() {
		super.destroy(); 
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		HttpSession session= request.getSession();
		String requestURI = request.getRequestURI();
		user u=(user)session.getAttribute("user");
		String userAgent = request.getHeader("User-Agent");
		boolean is_mobile = false;
		String[] mobileKeywords = {"Android", "iPhone", "iPad", "Windows Phone"};
		for (String keyword : mobileKeywords) {
		    if (userAgent.contains(keyword)) {
		    	is_mobile = true;
		        break;
		    }
		}
		request.setAttribute("is_mobile", is_mobile);
		if (requestURI.equals("/teamfight_wiki/edit_army")){
			info infos=am_service.get_all_info();
			request.setAttribute("info", infos);
			request.getRequestDispatcher("/army/edit_army.jsp").forward(request,response);
		}
		if (requestURI.equals("/teamfight_wiki/edit_army_msg")){
			if(u==null){
				request.getRequestDispatcher("/account_manage/login.jsp").forward(request,response);
			}
			String army_detail=request.getParameter("army_detail");
			String army_name=request.getParameter("army_name");
			am_service.save_army(army_name,army_detail,u.getUser_name());
			request.getRequestDispatcher("/my_army").forward(request,response);
		}
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}
}
